import "@ethersproject/shims";
import { ethers } from 'ethers';
import { getERC20Balance, getERC20GasData, getERC20TxData, getERC20TransferHistory } from './ERC20Transfer'
import { getETHBalance, transferETH, getETHGasData, getETHTxData, getETHTransferHistory, getTransactionDetail } from './ETHTransfer'
import { global } from "@/config/config";
import { getValue } from "../deviceStorage";

let rpcProvider
let rpcNode = getValue('RPCNode')
if (rpcNode && rpcNode.includes('http')) {
    rpcProvider = new ethers.providers.JsonRpcProvider(rpcNode);
} else {
    rpcProvider = new ethers.providers.JsonRpcProvider(global.RPCNode);
}
const httpsProvider = rpcProvider

const getCustomABI = (abi, funName, params) => {
    const masterABI = [abi]
    const masterIface = new ethers.utils.Interface(masterABI);
    const masterAbi = masterIface.encodeFunctionData(funName, params);
    return masterAbi
}

const executeCustomABI = async (masterAbi) => {
    const result = await httpsProvider.call({
        to: global.ERC20ContractAddress,
        data: masterAbi
    })
    return result
}

export {
    getCustomABI,
    executeCustomABI,
    getETHBalance,
    getERC20Balance,
    transferETH,
    getETHGasData,
    getERC20GasData,
    getERC20TxData,
    getETHTxData,
    getETHTransferHistory,
    getERC20TransferHistory,
    getTransactionDetail
}